<?php
/**
 * 后台Public控制器
  * PHP version 5
 * @package   Antonia
 * @author    Antonia <471699715@qq.com>
 * @copyright 2016 Antonia.
 */
namespace Admin\Controller;
use Think\Controller;
class PublicController extends Controller {


    /**
     * 如果通过认证跳转到首页
     *
     * @param HttpRequest $request
     *
     * @return Image
     * @author Antonia
    */
    public function index() {
        redirect(__APP__."/Admin");
    }

    /**
     * 发送邮件
     *
     * @param Array  $addrlist array(array("mail" => "", "name" => ""),.....)
     * @param String $content
     *
     * @return void
     * @author Antonia
     */
    public function sendMail($addrlist, $content) {

        $subject = "系统邮件";

        $set = M("Set")->find();

        Vendor('phpmailer.phpmailer');
        $mail = new PHPMailer();  

        $mail->IsSMTP();                  // 采用SMTP方式 
        $mail->Host = $set["smtpHost"];   // SMTP servers
        $mail->Port = $set["smtpPort"];
        $mail->SMTPAuth = true;           // 设置SMTP是否需要密码验证，true表示需要  
        $mail->Username = $set["smtpUser"];   
        $mail->Password = $set["smtpPass"];
        $mail->From = $set["smtpFrom"];
        $mail->FromName =  $set["smtpFromName"];  

        $mail->CharSet = "utf-8";  
        $mail->Encoding = "base64";

        foreach ($addrlist as $addr) {
            $mail->AddAddress($addr["mail"],$addr["name"]);  // 收件人邮箱和姓名 
        }

        $mail->IsHTML(true);
        $mail->Subject = $subject;
        // 邮件内容
        $mail->Body = '<html><head><meta http-equiv="Content-Language" content="zh-cn">'
                        .'<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>'
                        .'<body>'.$content.'</body></html>';       
        $mail->AltBody ="text/html";

        if(!$mail->Send()) {
            return array("stats" => 0, "msg"=> $mail->ErrorInfo);
        } else {    
            return array("stats" => 1, "msg"=> 'success');
        }
    }


    /**
     * 检查用户是否登录
     *
     * @param HttpRequest $request
     *
     * @return void
     * @author Antonia
    */
    public function checkUser() {
        if (!isset($_SESSION[C('USER_AUTH_KEY')])) {
            redirect(PHP_FILE . C('USER_AUTH_GATEWAY'));
        }
    }

    /**
     * 显示管理员登录页面
     *
     * @param HttpRequest $request
     *
     * @return void
     * @author Antonia
    */
    public function login() {
        if(!isset($_SESSION[C('USER_AUTH_KEY')])) {
            $this->display();
        }else{
            $this->redirect("/Admin");
        }
    }

    /**
     * 生成验证码
     *
     * @param HttpRequest $request
     *
     * @return Image
     * @author Antonia
    */
    Public function verify(){
        $Verify = new \Think\Verify();
        $Verify->fontSize = 30;
        $Verify->length = 4;
        $Verify->useNoise = false;
        $Verify->entry();
    }

    /**
     * 登录检测
     *
     * @param HttpRequest $request
     *
     * @return void
     * @author Antonia
    */
    public function checkLogin() {
        if (empty($_POST['admin_name'])) {
            $this->error('用户名或密码不正确!');
        } elseif (empty($_POST['admin_pass'])){
            $this->error('用户名或密码不正确!');
        }
        // } elseif ('' === trim($_POST['code'])){
        //     $this->error('验证码为必填项！');
        // }

        // if($_SESSION['verify'] != md5($_POST['code'])) {
        //     $this->error('验证码错误！');
        // }

        //生成认证条件
        $map            =   array();
        $map['admin_name'] = remove_xss($_POST['admin_name']);

        $Model = D('Admins');
        $authInfo = $Model->where($map)->find();
        
        if (false === $authInfo) {
            $this->error('用户名或密码不正确!');
        } else {
            $admin_pass = md5($_POST['admin_pass']);

            if ($authInfo['admin_pass'] != $admin_pass) {
                $this->error('用户名或密码不正确!');
            }

            $time = time();
            $ip   = get_client_ip();

            $_SESSION[C('USER_AUTH_KEY')]   =   $authInfo['admin_id'];
            $_SESSION[C('USER_AUTH_GROUP')] =   $authInfo['group_id'];
            $_SESSION['admin_name']         =   $authInfo['admin_name'];
            $_SESSION['name']               =   $authInfo['name'];
            $_SESSION['user_store_id']      =   $authInfo['store_id'];
            $_SESSION['lastLoginTime']      =   $time;

            //保存登录信息
            $data = array();
            $data['admin_id']        = $authInfo['admin_id'];
            $data['last_login_time'] = $time;
            $data['last_login_ip']   = $ip;
            $Model->save($data);
            $this->redirect('/Admin');
        }
    }

    /**
     * 管理员登出
     *
     * @param HttpRequest $request
     *
     * @return void
     * @author Antonia
    */
    public function logout() {
        if(isset($_SESSION[C('USER_AUTH_KEY')])) {
            unset($_SESSION[C('USER_AUTH_KEY')]);
            unset($_SESSION);
            session_destroy();
                        
            $this->assign("jumpUrl", __URL__.'/login/');
            $this->redirect('/Admin');
        }else {
            $this->error('已经登出！');
        }
    }



    /**
     * 更改登录密码
     *
     * @param HttpRequest $request
     *
     * @return void
     * @author Fee
    */
    protected function changePwd()
    {
        $this->checkUser();
        //对表单提交处理进行处理或者增加非表单数据
        if (md5($_POST['verify']) != $_SESSION['verify']) {
            $this->error('验证码错误！');
        }
        $map = array();
        $map['password']= md5($_POST['oldpassword']);
        if (isset($_POST['account'])) {
            $map['account'] = remove_xss($_POST['account']);
        } elseif (isset($_SESSION[C('USER_AUTH_KEY')])) {
            $map['id'] = $_SESSION[C('USER_AUTH_KEY')];
        }
        //检查用户
        $User  =  D("User");
        if(!$User->where($map)->field('id')->find()) {
            $this->error('旧密码不符或者用户名错误！');
        } else {
            $User->password = md5($_POST['password']);
            $User->save();
            $this->assign('jumpUrl', __APP__."/Admin".'/Public/main');
            $this->success('密码修改成功！');
         }
    }
}
?>